/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: HandlerContainer
 * @Package com.rx.core.mode
 * @Description: 处理器容器
 * @author: 陈锦韬
 * @date: 2021\6\11 0011
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.mode;

import com.rx.core.handler.RxHandlerWraper;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxSpecialUtil;
import lombok.Data;

import java.util.*;

/**
 * @author: Administrator
 * @Description: 处理器容器
 * 分成全部适用ALL,普通处理模式。
 * 每一模式容器都有三种处理器。前处理器prev 覆盖处理器over 后处理器。
 * 每一类处理器的key 有 ALL-通过 URL-有条件  EXT.URL-排除
 * @date: 2021\6\11 0011
 */

public class HandlerContainer {
    public static final String ALL_PREFIX="ALL";
    public static final String EXT_PREFIX="EXT_PREFIX";
    public static final Map<String,String> HANDLER_ALIAS = new HashMap<>();
    @Data
    public static class HandlerAllList{
        List<RxHandlerWraper> prevList;
        List<RxHandlerWraper> overList;
        List<RxHandlerWraper> afterList;

        public List<RxHandlerWraper> getHandlerList(Class anno){
            try {
                return (List<RxHandlerWraper>)RxSpecialUtil.AnnoForHandler(anno,
                        ()->prevList,
                        ()->overList,
                        ()->afterList);
            } catch (Throwable throwable) {
                return Collections.emptyList();
            }
        }
    }

    protected static List<RxHandlerWraper> containerNotNull(String key , Map<String, List<RxHandlerWraper>> container){
        List<RxHandlerWraper> list = container.get(key);
        if (EmptyChecker.isEmpty(list)){
            list = new ArrayList<>();
            container.put(key,list);
        }
        return list;
    }
    protected static Map<String, List<RxHandlerWraper>> containerByClass(Class cls,Map<String, List<RxHandlerWraper>> container,
                                                                         Map<String, List<RxHandlerWraper>> containerPrev,
                                                                         Map<String, List<RxHandlerWraper>> containerAfter){
        try {
            return (Map<String, List<RxHandlerWraper>>)RxSpecialUtil.AnnoForHandler(cls,
                    ()->containerPrev,
                    ()->container,
                    ()->containerAfter);
        } catch (Throwable throwable) {
            return Collections.emptyMap();
        }
    }

    /**
     * 根据别名获取真实处理器名称
     * @param alias
     * @return
     */
    public static String getHandlerName(String alias){
        if (EmptyChecker.isEmpty(alias)){
            return alias;
        }
        if (alias.contains(".")){
            return alias;
        }
        String name = HANDLER_ALIAS.get(alias);
        if(EmptyChecker.isEmpty(name)){
            return alias;
        }
        return name;
    }


}
